package h0801;

import java.util.*;
import java.util.function.Consumer;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 智能成绩表
 * @date 2024/8/1 17:05
 */
public class IntelligentScoreSheet {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //学生人数 n 和科目数量 m
        int n = sc.nextInt();
        int m = sc.nextInt();

        String[] subjects = new String[m];
        for (int i = 0; i < m; i++) {
            subjects[i] = sc.next();
        }
        List<List<String>> students = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            List<String> temp = new ArrayList<>();
            temp.add(sc.next());
            temp.add(sc.next());
            temp.add(sc.next());
            students.add(temp);
        }
//        sc.nextLine();
        String sub = sc.next();
        sortScores(sub, students, subjects);
    }

    private static void sortScores(String sub, List<List<String>> students, String[] subjects) {
        int sortIdx = 0;
        for (int i = 0; i < subjects.length; i++) {
            if (sub.equals(subjects[i])) {
                sortIdx = i + 1;
                break;
            }
        }
        if (sortIdx != 0) {
            int finalSortIdx = sortIdx;
            students.sort((o1, o2) -> {
                int a = Integer.parseInt(o1.get(finalSortIdx));
                int b = Integer.parseInt(o2.get(finalSortIdx));
                if (a!=b){
                    return Integer.compare(b, a); //从大到小排序
                }else {
                    return o1.get(0).compareTo(o2.get(0));
                }
            });
        } else {
            students.sort((o1, o2) -> {
                int a = o1.subList(1, o1.size()).stream().mapToInt(Integer::parseInt).sum();
                int b = o2.subList(1, o2.size()).stream().mapToInt(Integer::parseInt).sum();
                if (a!=b){
                    return Integer.compare(b,a); //倒序排列
                }else {
                    return o1.get(0).compareTo(o2.get(0));
                }
            });
        }

        StringJoiner joiner=new StringJoiner(" ");
        for (List<String> student : students) {
            joiner.add(student.get(0));
        }
        System.out.println(joiner);
    }
}
